#include "inimanager.h"

#include <QVariant>
#include <QString>
#include <QSettings>
#include <QDebug>

iniManager::iniManager(const QString &filePath, QObject *parent)
    : QObject(parent),
    m_filePath(filePath)
{

}

QStringList iniManager::splitString(const QString &input) {
    QRegExp regex(R"(^(\d+)_([\d]+)_([\d]+)_([\d]+)_(K|Z)$)");

    if (!regex.exactMatch(input)) {
        qWarning() << "工作组编号，格式不正确!";
        return QStringList();
    }

    QStringList parts = input.split('_');

    if (parts.size() != 5) {
        qWarning() << "分裂后超出数量!";
        return QStringList();
    }

    QString category = parts.last();
    if (category != "K" && category != "Z") {
        qWarning() << "无效，类型必须是 'K' 或 'Z'.";
        return QStringList();
    }

    return parts;
}

QString iniManager::getGroupIdQString(const uint16_t id, const uint16_t l, const uint16_t w, const uint16_t h, const QString type)
{
    if (type != "K" && type != "Z") {
        qWarning() << "无效，类型必须是 'K' 或 'Z'.";
        return "";
    }

    QStringList groupIdList;
    groupIdList << QString::number(id) << QString::number(l) << QString::number(w) << QString::number(h) << type;

    QString groupId = groupIdList.join("_");

    return groupId;
}

QString iniManager::getGroupType(const QString &type)
{
    QString trunType;

    if(type == "K")
    {
        trunType = "宽";
    } else if(type == "Z"){
        trunType = "窄";
    } else {
        trunType = "NULL";
    }
    return trunType;
}

bool iniManager::isExists(const QString &key, const QVariant &value) const
{
    QStringList groups = allGroups();

    for(const QString &group : groups)
    {
        QVariant groupValue = readValue(group, key);
        if(groupValue.isValid() && groupValue == value)
        {
            return true; // 如果找到匹配的值，立即返回true
        }
    }
    return false; // 如果未找到匹配的值，返回false
}

QVariant iniManager::readValue(const QString &group, const QString &key) const
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    QVariant value = settings.value(key);
    settings.endGroup();
    return value;
}

void iniManager::writeValue(const QString &group, const QString &key, const QVariant &value)
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    settings.setValue(key, value);
    settings.endGroup();
}

void iniManager::addGroup(const QString &group, const QVariantMap &keyValues)
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    for (auto it = keyValues.constBegin(); it != keyValues.constEnd(); ++it)
    {
        settings.setValue(it.key(), it.value());
    }
    settings.endGroup();
}

void iniManager::removeGroup(const QString &group)
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    settings.remove("");
    settings.endGroup();
}

void iniManager::removeValue(const QString &group, const QString &key)
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    settings.remove(key);
    settings.endGroup();
}

QStringList iniManager::allGroups() const
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    return settings.childGroups();
}

QStringList iniManager::allKeys(const QString &group) const
{
    QSettings settings(m_filePath, QSettings::IniFormat);
    settings.beginGroup(group);
    QStringList keys = settings.childKeys();
    settings.endGroup();
    return keys;
}
